﻿<Window x:Class="CodeIDX.Views.OptionsDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Converters="clr-namespace:CodeIDX.Views.Resources.Converters"
        xmlns:OptionViewModels="clr-namespace:CodeIDX.ViewModels.Options"
        xmlns:Settings="clr-namespace:CodeIDX.Settings"
        xmlns:tookit="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="Options"
        Width="500"
        Height="400"
        MinWidth="350"
        MinHeight="300"
        Background="#F0F0F0"
        ResizeMode="NoResize">
    <Window.Resources>
        <Style TargetType="CheckBox">
            <Setter Property="Margin" Value="0,0,0,3" />
        </Style>
        <Style TargetType="RadioButton">
            <Setter Property="Margin" Value="0,0,0,3" />
        </Style>

        <!--  General Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:GeneralOptionsViewModel}">
            <StackPanel Margin="5"
                        Orientation="Vertical">
                <CheckBox Content="Start CodeIDX on system startup"
                          IsChecked="{Binding StartOnSystemStartup}" />
                <CheckBox x:Name="cbShowTrayIcon"
                          Content="Show tray icon"
                          IsChecked="{Binding ShowTrayIcon}" />
                <StackPanel Margin="25,0,0,0"
                            IsEnabled="{Binding IsChecked,
                                                ElementName=cbShowTrayIcon}">
                    <CheckBox Content="Minimize to tray"
                              IsChecked="{Binding MinimizeToTray}" />
                    <CheckBox Content="Exit to tray"
                              IsChecked="{Binding ExitToTray}" />
                    <CheckBox Content="Single click tray"
                              IsChecked="{Binding SingleClickTray}" />
                </StackPanel>

                <CheckBox Content="Load last index on startup"
                          IsChecked="{Binding LoadLastIndexOnStartup}" />
                <Grid Margin="0,0,0,3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="10" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock VerticalAlignment="Center"
                               Text="Refresh index at startup:" />
                    <ComboBox Grid.Column="2"
                              IsReadOnly="True"
                              SelectedValue="{Binding RefreshIndexAtStartup}"
                              SelectedValuePath="Tag">
                        <ComboBoxItem Content="Never"
                                      Tag="{x:Static Settings:RefreshAtStartupKind.Never}" />
                        <ComboBoxItem Content="First of the day"
                                      Tag="{x:Static Settings:RefreshAtStartupKind.FirstStartup}"
                                      ToolTip="Refresh when loading for the first time of the day" />
                        <ComboBoxItem Content="Always"
                                      Tag="{x:Static Settings:RefreshAtStartupKind.Always}"
                                      ToolTip="Always refresh when loading the index" />
                    </ComboBox>
                </Grid>
            </StackPanel>
        </DataTemplate>

        <!--  Index Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:IndexOptionsViewModel}">
            <StackPanel Margin="5"
                        Orientation="Vertical">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <CheckBox VerticalAlignment="Center"
                              Content="Disable index optimization"
                              IsChecked="{Binding DisableOptimizeIndex}"
                              ToolTip="(Recommended) Disable manual index optimization.&#10;Disable for better performance when indexing many files.&#10;The index will still be optimized in small steps from time to time." />
                    <Button Grid.Column="1"
                            Click="OptimizeNow_Click"
                            Content="Optimize Now"
                            ToolTip="Optimize manually in case you're experiencing noticable performance issues.&#10;Optimizing will increase the search performance.&#10;This will take some time depending on the index size." />
                </Grid>
            </StackPanel>
        </DataTemplate>

        <!--  Search Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:SearchOptionsViewModel}">
            <StackPanel Margin="5"
                        Orientation="Vertical">
                <GroupBox Margin="0,0,0,3"
                          Header="Paging"
                          Padding="3">
                    <StackPanel Orientation="Vertical">
                        <Grid Margin="0,3">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="3" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <TextBlock VerticalAlignment="Center"
                                       Text="Min Page Size" />
                            <tookit:IntegerUpDown Grid.Column="2"
                                                  Width="80"
                                                  Increment="100"
                                                  Maximum="10000"
                                                  Minimum="100"
                                                  Value="{Binding PageSize}" />
                        </Grid>

                        <CheckBox Content="Load all items when background search is finished"
                                  IsChecked="{Binding LoadRemainingLazyResults}"
                                  ToolTip="After displaying the first results, the search continues in the background." />
                    </StackPanel>
                </GroupBox>

                <GroupBox Margin="0,0,0,3"
                          Header="New Search"
                          Padding="3">
                    <StackPanel Orientation="Vertical">
                        <CheckBox Content="Enable filter"
                                  IsChecked="{Binding EnableFilterByDefault}" />
                        <CheckBox Content="Insert text from clipboard"
                                  IsChecked="{Binding InsertTextFromClipBoard}" />
                    </StackPanel>
                </GroupBox>

                <GroupBox Margin="0,0,0,3"
                          Header="Filter"
                          Padding="3">
                    <StackPanel Orientation="Vertical">
                        <CheckBox Content="Show directory filter"
                                  IsChecked="{Binding EnableDirectoryFilter}" />
                        <CheckBox Content="Show file filter"
                                  IsChecked="{Binding EnableFileFilter}" />
                    </StackPanel>
                </GroupBox>

                <CheckBox Content="Enable search history"
                          IsChecked="{Binding EnableSearchHistory}" />
                <CheckBox Content="Enable search in results"
                          IsChecked="{Binding EnableSearchInResults}" />
            </StackPanel>
        </DataTemplate>

        <!--  Results Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:ResultOptionsViewModel}">
            <StackPanel Margin="5"
                        Orientation="Vertical">

                <CheckBox Content="Select match in preview"
                          IsChecked="{Binding SelectMatchInPreview}" />
                <CheckBox Content="Edit match on double click"
                          IsChecked="{Binding EnableEditMatchOnDoubleClick}" />
                <CheckBox Content="Filter file on Enter"
                          IsChecked="{Binding FilterFileOnEnter}"
                          ToolTip="Default behavior is open file" />

                <GroupBox Margin="0,0,0,3"
                          Header="Default Editor"
                          Padding="3"
                          ToolTip="Select the default editor to use when double clicking a search result.">
                    <StackPanel>
                        <RadioButton Content="Default"
                                     GroupName="defaultTool"
                                     IsChecked="{Binding UseDefaultEditorAsDefault}"
                                     ToolTip="Open a match in the default editor of the result file type." />
                        <RadioButton Content="Visual Studio"
                                     GroupName="defaultTool"
                                     IsChecked="{Binding UseVisualStudioAsDefault}"
                                     ToolTip="Open a match in Visual Studio by default&#10;This option may cause issues on some systems. In that case disable it." />
                        <RadioButton Content="Notepad"
                                     GroupName="defaultTool"
                                     IsChecked="{Binding UseNotepadAsDefault}"
                                     ToolTip="Open a match in Notepad by default&#10;If Notepad++ is installed, it is opened instead." />
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="3" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <RadioButton VerticalAlignment="Center"
                                         Content="Custom"
                                         GroupName="defaultTool"
                                         IsChecked="{Binding UseCustomEditorAsDefault}"
                                         ToolTip="Open a match in a custom editor.&#x0a;arguments:&#x0a;$file - file path&#x0a;$directory - directory path&#x0a;$line - line number&#x0a;e.g. notepad++ $file -n$line" />
                            <TextBox Grid.Column="2"
                                     Text="{Binding DefaultEditorCommandLineOptions}"
                                     ToolTip="Open a match in a custom editor.&#x0a;arguments:&#x0a;$file - file path&#x0a;$directory - directory path&#x0a;$line - line number&#x0a;e.g. notepad++ $file -n$line" />
                        </Grid>
                    </StackPanel>
                </GroupBox>
            </StackPanel>
        </DataTemplate>

        <!--  UserInterface Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:UIOptionsViewModel}">
            <StackPanel Margin="5"
                        Orientation="Vertical">

                <CheckBox Content="Show file count in results"
                          IsChecked="{Binding ShowResultFileCount}"
                          ToolTip="Format: (Result count : File count)" />
                <CheckBox Content="Load last searches"
                          IsChecked="{Binding LoadLastSearches}" />

            </StackPanel>
        </DataTemplate>

        <!--  Blacklist Template  -->
        <DataTemplate DataType="{x:Type OptionViewModels:BlacklistOptionsViewModel}">
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="3" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="3" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0"
                           Text="Blacklist directories" />

                <ScrollViewer Grid.Row="2"
                              Grid.Column="0"
                              HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Auto">
                    <TextBox AcceptsReturn="True"
                             Text="{Binding Directories,
                                            Converter={x:Static Converters:ListToStringConverter.Instance},
                                            UpdateSourceTrigger=LostFocus}"
                             TextWrapping="NoWrap"
                             ToolTip="One or more directories to exclude from the search&#10;Use 'Temp' to filter any directory or subdirectory named 'Temp'" />
                </ScrollViewer>

                <Button Grid.Row="2"
                        Grid.Column="2"
                        VerticalAlignment="Top"
                        Click="AddBlacklistDirectory_Click"
                        Content="…"
                        Padding="3,0"
                        ToolTip="Add directory" />
            </Grid>
        </DataTemplate>
    </Window.Resources>

    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TreeView x:Name="OptionsTree"
                  Grid.Column="0"
                  Background="#F5F5F5">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsSelected">
                        <Setter.Value>
                            <MultiBinding Converter="{x:Static Converters:ItemTypeEqualsTypeConverter.Instance}"
                                          Mode="OneTime">
                                <Binding Mode="OneWay" />
                                <Binding Mode="OneWay"
                                         Path="DataContext.LastOptionsPageType"
                                         RelativeSource="{RelativeSource Mode=FindAncestor,
                                                                         AncestorType={x:Type TreeView}}" />
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeViewItem DataContext="{Binding General}"
                          Header="General" />
            <TreeViewItem DataContext="{Binding Index}"
                          Header="Index" />
            <TreeViewItem DataContext="{Binding UserInterface}"
                          Header="UI" />
            <TreeViewItem DataContext="{Binding Search}"
                          Header="Search" />
            <TreeViewItem DataContext="{Binding Results}"
                          Header="Results" />
            <TreeViewItem DataContext="{Binding Blacklist}"
                          Header="Blacklist" />
        </TreeView>

        <Grid Grid.Column="2">
            <ContentControl Content="{Binding SelectedItem.DataContext, ElementName=OptionsTree}" />
        </Grid>

        <Grid Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="5" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <GridSplitter Grid.Row="2"
                          Height="1"
                          HorizontalAlignment="Stretch"
                          Background="#828790" />
        </Grid>

        <Grid Grid.Row="2"
              Grid.ColumnSpan="3"
              HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="85" />
                <ColumnDefinition Width="5" />
                <ColumnDefinition Width="85" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="1"
                    Click="OK_Click"
                    Content="OK"
                    IsDefault="True" />
            <Button Grid.Column="3"
                    Content="Cancel"
                    IsCancel="True" />
        </Grid>
    </Grid>
</Window>
